package cn.org.xiaoweiba.graduationdesign.bookmall.controller;

import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.mail.MailLegalAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.mail.MailLegalObjectAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.mail.MailLegalParameterAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.rsa.DecodeRsaCommonAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.rsa.DecodeRsaCommonObjectAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.rsa.DecodeRsaCommonParameterAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.string.StringNotNullAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.string.StringNotNullObjectAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.annotation.string.StringNotNullParameterAnnotation;
import cn.org.xiaoweiba.graduationdesign.bookmall.dto.result.BaseResponseResult;
import cn.org.xiaoweiba.graduationdesign.bookmall.dto.result.OkResponseResult;
import cn.org.xiaoweiba.graduationdesign.bookmall.service.LoginService;
import cn.org.xiaoweiba.graduationdesign.bookmall.vo.UserLoginInfoByMailVo;
import cn.org.xiaoweiba.graduationdesign.bookmall.vo.UserLoginInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 登录控制器
 */
@RestController
@RequestMapping("/login")
@Tag(name = "登录", description = "和登录相关的 API")
public class LoginController {

    /**
     * 登录业务层对象
     */
    @Autowired
    private LoginService loginService;

    /**
     * 发送邮箱验证码。
     * 用户通过输入的邮箱获取登录时使用的验证码。
     * 控制器方法接收的邮箱为加密的，需要使用 DecodeRsaCommonAnnotation
     * 标注方法、DecodeRsaCommonParameterAnnotation 标注参数通过 AOP
     * 进行 RSA 解密。
     *
     * @param mail 用户输入的邮箱
     * @return 结果响应对象
     */
    @PostMapping("/mail/code")
    @Operation(
            summary = "发送邮箱验证码",
            description = "用户通过输入的邮箱获取登录时使用的验证码"
    )
    @StringNotNullAnnotation
    @DecodeRsaCommonAnnotation
    @MailLegalAnnotation
    public BaseResponseResult<Object> sendMailCode(
            @RequestParam("mail")
            @StringNotNullParameterAnnotation
            @DecodeRsaCommonParameterAnnotation
            @MailLegalParameterAnnotation
            String mail
    ) {
        return loginService.sendMailCode(mail);
    }

    /**
     * 用户通过电子邮箱进行登录
     *
     * @param userLoginInfoVo 用户登录信息
     * @return 响应结果对象
     */
    @PostMapping("/mail")
    @Operation(
            summary = "邮箱登录",
            description = "用户通过邮箱进行登录"
    )
    @StringNotNullAnnotation
    @DecodeRsaCommonAnnotation
    @MailLegalAnnotation
    public BaseResponseResult<Object> loginByMail(
            @RequestBody
            @StringNotNullObjectAnnotation
            @DecodeRsaCommonObjectAnnotation
            @MailLegalObjectAnnotation
            UserLoginInfoByMailVo userLoginInfoVo
    ) {
        return loginService.loginByEmail(userLoginInfoVo);
    }

    @GetMapping("/test")
    @DecodeRsaCommonAnnotation
    public void test(
            @DecodeRsaCommonParameterAnnotation
            String text
    ) {
        System.out.println(text);
    }

}
